// pages/user/address/index/index.js

import Dialog from '../../../../dist/dialog/dialog';
import server from '../../../../utils/server';
Page({

  /**
  * 页面的初始数据
  */
  data: {
    array: [],
    id: '',
    show: 'true',
    detailAddress: '',
    mobile: '',
    username: '',
    addressId:'',
    goodsId:''
  },

  //点击编辑跳转到地址编辑页面
  toEditorPage(e) {
    let id = e.currentTarget.id
    wx.navigateTo({
      //通过路径传对应的id值，以便接下来查对应的字段值
      url: '/pages/user/address/editor/editor?id=' + id,
    });
  },

  //跳转到新增页面
  toAddPage() {
    wx.navigateTo({
      url: '/pages/user/address/newadd/add',
    })
  },



   toMyAddressPage(e) {
        let that = this;
        let id = e.currentTarget.id
    
        that.setData({
          addressId:id
        })
    
        //携带参数跳转到 结算页面
        wx.navigateTo({
          url: '/pages/checkout/checkout',
    
          // 向结算页面传入地址id和商品id
          success(res) {
            res.eventChannel.emit('myaddressToCheckout',{
              data:{
                addressId:that.data.addressId,
                goodsId:that.data.goodsId
              }
            })
          }
        })
      },


  //点击选择按钮跳转到结算页面
  toMyAddressPage1(e) {
    let that = this;
    let id = e.currentTarget.id

/*     that.setData({
      addressId:id
    })

    //携带参数跳转到 结算页面
    wx.navigateTo({
      url: '/pages/checkout/checkout', */

      // 向结算页面传入地址id和商品id
    //根据id 请求后端 获取该单元格对应的地址信息
    var openid = wx.getStorageSync("openId");
    const token = wx.getStorageSync('token');
    wx.request({
      header:{
        "Authorization": token
      },
      url: server + 'addresss/selectAddressInfoById/' + id,
      success(res) {
        res.eventChannel.emit('myaddressToCheckout',{
          data:{
            addressId:that.data.addressId,
            goodsId:that.data.goodsId
          }
        })
      }
    })
  },

  deleteAddress(e) {
    let that = this;
    let id = e.currentTarget.id;
    console.log(id);
    Dialog.confirm({
      message: '确认删除该收获地址吗？',
    })
      .then(() => {
        // on confirm
        //确认删除 --微信小程序删除该单元格 并提供id给后端接口 发送请求删除数据库中的地址
        var openid = wx.getStorageSync("openId");
        const token = wx.getStorageSync('token');
        wx.request({
          header:{
            "Authorization": token
          },
          url: server + 'addresss/deleteAddressById/' + id,
          success(res) {
            if (res.data.code == 200) {
              wx.showToast({
                title: '删除成功！',
              })
              const openid = wx.getStorageSync("openId");
              const token = wx.getStorageSync('token');
              //删除成功后，将数据库新的地址列表重新渲染！！！
              wx.request({
                header:{
                  "Authorization": token
                },
                url: server + 'addresss/list/' + openid,
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success(res) {
                  console.log(res.data.rows)
                  //将返回的数组赋值给array
                  that.setData({
                    array: res.data.rows,
                  })
                }
              })
            }
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.setNavigationBarTitle({
      title: '我的收获地址'
    })

    // 监听跳转传值，获取结算页面传来的goodsId
    const eventChannel1 = this.getOpenerEventChannel()
    eventChannel1.on('checkOutToMyaddress',function (data) {
      console.log(data.data.goodsId)
      that.setData({
        goodsId:data.data.goodsId
      })
    })
    const openid = wx.getStorageSync("openId");
    const token = wx.getStorageSync('token');
    wx.request({
      //请求获取地址列表接口
      header:{
        "Authorization": token,
        'content-type': 'application/json' // 默认值
      },
      url: server + 'addresss/list/' + openid,
      success(res) {
        console.log(res.data.rows)
        //将返回的数组赋值给array
        that.setData({
          array: res.data.rows,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})